/**
 * @author Sergey Kolchin <ksa242@gmail.com>
 */

using System;
using System.Collections;
using System.Globalization;


namespace DeathByCaptcha
{
    public class User
    {
        protected int _id = 0;
        protected bool _banned = false;
        protected double _balance = 0.0;


        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = Math.Max(0, value);
                _balance = 0.0;
                _banned = false;
            }
        }

        public double Balance
        {
            get
            {
                return _balance;
            }
        }

        public bool LoggedIn
        {
            get
            {
                return 0 < _id;
            }
        }

        public bool Banned
        {
            get
            {
                return LoggedIn && _banned;
            }
        }


        public User()
        {}

        public User(Hashtable src) : this()
        {
            if (null != src && src.ContainsKey("user")) {
                try {
                    this.Id = Convert.ToInt32(src["user"]);
                } catch (System.Exception) {
                    //
                }
                if (this.LoggedIn) {
                    try {
                        this._balance = Convert.ToDouble(src["balance"]);
                    } catch (System.Exception) {
                        //
                    }
                    try {
                        this._banned = Convert.ToBoolean(src["is_banned"]);
                    } catch (System.Exception) {
                        //
                    }
                }
            }
        }


        public int ToInt()
        {
            return this._id;
        }

        public bool ToBoolean()
        {
            return this.LoggedIn && !this.Banned;
        }
    }
}
